<?php

class MakeMemcache
{
    //****START:创建新缓存****//
    static function new_memcache($MemcacheName, $MakeMemo)
    {//ArrayType:0=一维；1=二维
        global $mc;

        if ($MakeMemo <> "") {
            $MakeState = memcache_set($mc, $MemcacheName, $MakeMemo);//, 0, 2592000
        } else {
            $MakeState = memcache_delete($mc, $MemcacheName);
        }
        if ($MakeState) {//成功
            $intMakeState = 1;
        } else {//失败
            $intMakeState = 0;
        }
        //error_log($MemcacheName."\n", 3, "/www/logs/MCN".date("Ymd").".txt");
        return $intMakeState;
    }
    //****END:创建新缓存****//

    //****START:更新缓存****//
    static function update_memcache($MemcacheName, $ArrayType = 0, $MakeID, $MakeMemo)
    {//ArrayType:0=一维；1=二维
        global $mc;
        $GetMemcacheInfo = memcache_get($mc, $MemcacheName);
        if (trim($GetMemcacheInfo) <> "") {
            if ($ArrayType == 0) {
                $GetMemcacheInfo = array_merge($GetMemcacheInfo, $MakeMemo);
            } else {
                $intState = 0;
                for ($i = 0; $i < count($GetMemcacheInfo); $i++) {
                    if ($GetMemcacheInfo[$i]['ID'] == $MakeID) {
                        $intState = 1;
                        $GetMemcacheInfo[$i] = array_merge($GetMemcacheInfo[$i], $MakeMemo);
                    }
                }
                if ($intState == 0) {
                    $GetMemcacheInfo[] = $MakeMemo;
                }
            }
        } else {
            if ($ArrayType == 0) {
                $GetMemcacheInfo = $MakeMemo;
            } else {
                $GetMemcacheInfo[] = $MakeMemo;
            }
        }
        $MakeState = memcache_set($mc, $MemcacheName, $GetMemcacheInfo, 0, 2592000);
        if ($MakeState) {//成功
            $intMakeState = 1;
        } else {//失败
            $intMakeState = 0;
        }
        //error_log($MemcacheName."\n", 3, "/www/logs/MCN".date("Ymd").".txt");
        return $intMakeState;
    }
    //****END:更新缓存****//

    //****START:删除缓存****//
    static function delete_memcache($MemcacheName, $ArrayType = 0, $MakeID)
    {//ArrayType:0=一维；1=二维
        global $mc;
        $GetMemcacheInfo = memcache_get($mc, $MemcacheName);
        if (trim($GetMemcacheInfo) <> "") {
            if ($ArrayType == 0) {
                $GetMemcacheInfo = "";
            } else {
                $intState = 0;
                $n = 0;
                $tmpMemo = "";
                for ($i = 0; $i < count($GetMemcacheInfo); $i++) {
                    if ($GetMemcacheInfo[$i]['ID'] <> $MakeID) {
                        $tmpMemo[$n] = $GetMemcacheInfo[$i];
                        $n++;
                    }
                }
                $GetMemcacheInfo = $tmpMemo;
            }
        }
        if ($GetMemcacheInfo == "") {
            $MakeState = memcache_delete($mc, $MemcacheName);
        } else {
            $MakeState = memcache_set($mc, $MemcacheName, $GetMemcacheInfo, 0, 2592000);
        }
        if ($MakeState) {//成功
            $intMakeState = 1;
        } else {//失败
            $intMakeState = 0;
        }
        return $intMakeState;
    }
    //****END:更新缓存****//

    //****START:简单读取缓存****//
    static function get_memcache_small($MemcacheName)
    {
        global $mc;
        $strMemo = "";
        $strMemo = memcache_get($mc, $MemcacheName);
        return $strMemo;
    }
    //****END:简单读取缓存****//

    //****START:读取缓存****//
    static function get_memcache($MemcacheName, $ArrayType, $MakeID, $Type, $SendType, $page, $pagelist, $OrderName, $OrderType, $OrderMake, $ShowHide = 0)
    {//ArrayType:0=一维；1=二维 OrderType:DESC;ASC OrderMake:str;num
        global $mc;
        $strMemo = "";
        $GetMemcacheInfo = memcache_get($mc, $MemcacheName);
        $Max = count($GetMemcacheInfo);
        if (trim($GetMemcacheInfo) <> "") {
            if ($MakeID > 0) {//单条数据
                for ($i = 0; $i < $Max; $i++) {//读取数据
                    if ($GetMemcacheInfo[$i]['ID'] == $MakeID) {
                        if ($ShowHide > 0 && $GetMemcacheInfo[$i]['S'] == 0) {//过滤权限
                            if ($Type > 0) {
                                if ($GetMemcacheInfo[$i]['Y'] == $Type) {
                                    $strMemo = $GetMemcacheInfo[$i];
                                }
                            } else {
                                $strMemo = $GetMemcacheInfo[$i];
                            }
                        } else {
                            if ($Type > 0) {
                                if ($GetMemcacheInfo[$i]['Y'] == $Type) {
                                    $strMemo = $GetMemcacheInfo[$i];
                                }
                            } else {
                                $strMemo = $GetMemcacheInfo[$i];
                            }
                        }
                        break;
                    }
                }
                if (trim($strMemo) <> "") {
                    $strMemo['Max'] = $Max;
                }
            } else {//多条数据
                if ($ArrayType == 0) {//一维
                    $strMemo = $GetMemcacheInfo;
                } else {//二维
                    if (trim($OrderName) <> "" && trim($GetMemcacheInfo) <> "") {
                        if ($OrderMake == "num") {
                            $GetMemcacheInfo = Util::array_sort($GetMemcacheInfo, $OrderName, $OrderType);
                        } else {
                            $GetMemcacheInfo = Util::sortmddata($GetMemcacheInfo, $OrderName, $OrderType, $OrderMake);    //对数组排序
                        }
                    }
                    if ($Type > 0) {//过滤生活模块
                        $tmpMemo = "";
                        $n = 0;
                        for ($i = 0; $i < $Max; $i++) {
                            if ($GetMemcacheInfo[$i]['Y'] == $Type) {
                                $tmpMemo[$n] = $GetMemcacheInfo[$i];
                                $n++;
                            }
                        }
                        $GetMemcacheInfo = "";
                        $GetMemcacheInfo = $tmpMemo;
                    }
                    if ($ShowHide > 0 && $GetMemcacheInfo <> "") {//过滤权限设置
                        $tmpMemo = "";
                        $n = 0;
                        if (trim($GetMemcacheInfo) <> "") {
                            for ($i = 0; $i < count($GetMemcacheInfo); $i++) {
                                if (intval($GetMemcacheInfo[$i]['S']) == 0) {
                                    $tmpMemo[$n] = $GetMemcacheInfo[$i];
                                    $n++;
                                }
                            }
                        }
                        $GetMemcacheInfo = "";
                        $GetMemcacheInfo = $tmpMemo;
                    }
                    if (trim($GetMemcacheInfo) <> "") {
                        $Max = count($GetMemcacheInfo);
                    } else {
                        $Max = 0;
                    }
                    if ($Max > 0) {
                        if ($page == 0) {//取多少条
                            $intBegin = 0;
                            if ($pagelist == 0) {
                                $intEnd = $Max;
                            } else if ($pagelist > $Max) {
                                $intEnd = $Max;
                            } else {
                                $intEnd = $pagelist;
                            }
                        } else {//分页
                            $page--;
                            $intBegin = $pagelist * $page;                //开始
                            $intEnd = $intBegin + $pagelist;            //结束
                            $pagesum = round($Max / $pagelist + 0.49);        //总共多少页
                        }

                        $n = 0;
                        if ($Max <= $intEnd && $Max > $intBegin) {
                            $intEnd = $Max;
                        }
                        $strMemo = "";
                        for ($i = $intBegin; $i < $intEnd; $i++) {//读取数据
                            $strMemo[$n] = $GetMemcacheInfo[$i];
                            $n++;
                        }
                        if (trim($strMemo) <> "") {
                            $strMemo[0]['max'] = $Max;
                            if ($pagesum > 0) {
                                $strMemo[0]['maxpage'] = $pagesum;
                            } else {
                                $strMemo[0]['maxpage'] = 0;
                            }
                        }
                    }
                }
            }
        }

        return $strMemo;
    }
    //****END:读取缓存****//

    //****START:对数据换页排序****//
    static function order_array_page($strArrMemo, $page, $pagelist, $OrderName, $OrderType, $OrderMake)
    {//ArrayType:0=一维；1=二维 OrderType:DESC;ASC OrderMake:str;num
        global $mc;
        $strMemo = "";
        $Max = count($strArrMemo);

        if (trim($strArrMemo) <> "") {
            if (trim($OrderName) <> "" && trim($OrderType) <> "" && trim($OrderMake) <> "") {
                if ($OrderMake == "num") {
                    $strArrMemo = Util::array_sort($strArrMemo, $OrderName, $OrderType);
                } else {
                    $strArrMemo = Util::sortmddata($strArrMemo, $OrderName, $OrderType, $OrderMake);    //对数组排序
                }
            }

            $page--;
            $intBegin = $pagelist * $page;                //开始
            $intEnd = $intBegin + $pagelist;            //结束
            $pagesum = round($Max / $pagelist + 0.49);        //总共多少页

            $n = 0;
            if ($Max <= $intEnd && $Max > $intBegin) {
                $intEnd = $Max;
            }

            if ($intBegin >= $Max) {
                $strMemo = "";
            } else {
                for ($i = $intBegin; $i < $intEnd; $i++) {//读取数据
                    $strMemo[$n] = $strArrMemo[$i];
                    $n++;
                }
            }

            if (trim($strMemo) <> "") {
                $strMemo[0]['max'] = $Max;
                $strMemo[0]['maxpage'] = $pagesum;
            }
        }

        return $strMemo;
    }

    //****END:对数据换页排序****//

    static function redis_update($UserID, $StrID, $StrValue)
    {//更新Redis内容
        global $db, $mc;
        global $Redis_Memo;

        $strmemo = "";
        $redis = new Redis();
        if ($redis->connect($Redis_Memo['host'], $Redis_Memo['port']) == true) {
            /* user:password 拼接成AUTH的密码 */
            if ($redis->auth($Redis_Memo['user'] . ":" . $Redis_Memo['pass']) == true) {
                if ($redis->set($StrID, $StrValue) == true) {
                    $strmemo = "ok";
                } else {
                    $strmemo = "赋值错误";
                }
            }//END USER LINK
            else {
                $strmemo = "用户连接错误";
            }
        }//END LINK
        else {
            $strmemo = "连接服务器错误";
        }
        return $strmemo;
    }

    static function redis_get($UserID, $StrID)
    {//获取Redis内容
        global $db, $mc;
        global $Redis_Memo;

        $strmemo = "";
        $redis = new Redis();
        if ($redis->connect($Redis_Memo['host'], $Redis_Memo['port']) == true) {
            /* user:password 拼接成AUTH的密码 */
            if ($redis->auth($Redis_Memo['user'] . ":" . $Redis_Memo['pass']) == true) {
                $strmemo = $redis->get($StrID);
            }//END USER LINK
        }//END LINK

        return $strmemo;
    }
}

?>